package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.api.wemedia.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.impl.WmUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {
    @Autowired
    private WmUserServiceImpl service;

    @PostMapping("/save")
    @Override
    public ResponseResult save(@RequestBody WmUser wmUser) {
        if (wmUser == null || wmUser.getName() == null)return service.errorResult;
        service.save(wmUser);
        return service.sucResult;
    }

    @GetMapping("/findByName/{name}")
    @Override
    public WmUser findByName(@PathVariable("name") String name) {
        if (name == null) return null;
        /*
        1.LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmUser::getName,name);

        2.QueryWrapper<WmUser> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        wrapper.lambda().eq(WmUser::getName,name);

        3.Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name)
        */
        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();
        List<WmUser> list = service.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        return list.get(0);
    }
}
